package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.util.SsmUtil;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/sms")
public class SmsController {
    @Autowired
    private RedisService redisService;


    @PostMapping("sendVerifyCode")
    public R sendVerifyCode(String phone) {
        if(phone == null|| phone.length() != 11){
            throw new RuntimeException("手机号格式错误,请输入正确格式的手机号");
        }
      // String code = UUID.randomUUID().toString().replace("-", "").substring(0,6);
        Random random = new Random();
        Integer code1 = random.nextInt(900000) + 100000;
        String code = code1.toString();
       // SsmUtil.sendSmsAli(phone,code);
       System.err.println("验证码："+code);
       //存储验证码到redis
        //调用travel-common-redis中的RedisKeys枚举类拼接key的前缀和设置过期时间
        /*为何要拼接前缀：
        * 方便我们在查询redis中，只查出手机号和验证码对应的值进行过滤删除
        * */
        String key = RedisKeys.VERIFY_CODE.join(phone);
        //存储验证码到redis中
        redisService.setCacheObject(key,code,RedisKeys.VERIFY_CODE.getTime(), TimeUnit.SECONDS);

        return R.ok();
    }
}
